#include <iostream>
#include <unistd.h>
#include "RingQueue.hpp"

using namespace std;

int n=1;

void *Product(void *avg)
{
    RingQueue<int> *Bq = static_cast<RingQueue<int> *>(avg);
    for (int i = 1; i <= 15; i++)
    {
        // sleep(2);
        Bq->Equeue(n++);
        cout << "生产的值:" << n << endl;
    }
    return nullptr;
}
void *Consume(void *avg)
{
    RingQueue<int> *Bq = static_cast<RingQueue<int> *>(avg);
    int t = 0;
    for (int i = 0; i < 15; i++)
    {
       // sleep(2);
        Bq->Pop(t);
        cout << "消费的值: " << t << endl;
    }
    return nullptr;
}

int main()
{
    RingQueue<int> *Bq = new RingQueue<int>();
    vector<pthread_t> q,c;
    for(int i=0;i<5;i++)
    {
        pthread_t tid;
        q.push_back(tid);
        pthread_create(&tid,nullptr, Product, Bq);
    }
    for(int i=0;i<5;i++)
    {
        pthread_t tid;
        c.push_back(tid);
        pthread_create(&tid, nullptr, Consume, Bq);
    }
    for(auto x:q)
    {
        pthread_join(x, nullptr);
    }
    cout<<"生产者全部回收完毕"<<endl;
    for(auto x:c)
    {
        pthread_join(x, nullptr);
    }
    cout<<"消费者全部回收完毕"<<endl;
    return 0;
}